package com.muyu.controller;

import com.muyu.annotation.SysLog;
import com.muyu.entity.ResponseEntity;
import com.muyu.entity.UserEntity;
import com.muyu.enums.LogTypeEnum;
import com.muyu.requset.SaveUserRequest;
import com.muyu.requset.UpdateUserRequest;
import com.muyu.result.ResultResponse;
import com.muyu.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: UserController
 * @Author: Zirun.Xin
 * @Date: 2025/3/20 下午2:42
 * @Version: 1.0
 * @Description: 用户控制层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/api/v1/user")
@Api(tags = {"用户模块"})
public class UserController {

    /**
     * 用户业务层
     */
    private final UserService userService;

    /**
     * 添加用户
     * @param request 请求体
     * @return 响应结果集
     */
    @PostMapping(value = "/save")
    @SysLog(operation = "添加用户", logType = LogTypeEnum.LOG_TYPE_ADD)
    @ApiOperation(value = "添加用户", notes = "添加一个用户到数据库")
    public ResponseEntity<ResultResponse<Void>> saveUser(@RequestBody SaveUserRequest request) {
        userService.save(SaveUserRequest.convertToUserEntity(request));
        return ResponseEntity.ok(ResultResponse.ok(null));
    }

    /**
     * 删除用户
     * @param userId 用户id
     * @return 响应结果集
     */
    @DeleteMapping(value = "/remove")
    @SysLog(operation = "删除用户", logType = LogTypeEnum.LOG_TYPE_DELETE)
    @ApiOperation(value = "删除用户", notes = "根据用户id删除用户信息")
    public ResponseEntity<ResultResponse<Void>> removeUser(@ApiParam(value = "用户id", required = true)
                                                           @RequestParam(value = "userId") Integer userId) {
        userService.removeById(userId);
        return ResponseEntity.ok(ResultResponse.ok(null));
    }

    /**
     * 更新用户
     * @param request 请求体
     * @return 响应结果集
     */
    @PutMapping(value = "/update")
    @SysLog(operation = "更新用户", logType = LogTypeEnum.LOG_TYPE_EDIT)
    @ApiOperation(value = "更新用户", notes = "根据用户id更新用户信息")
    public ResponseEntity<ResultResponse<Void>> updateUser(@RequestBody UpdateUserRequest request) {
        userService.updateById(UpdateUserRequest.convertToUserEntity(request));
        return ResponseEntity.ok(ResultResponse.ok(null));
    }

    /**
     * 获取用户
     * @return 响应结果集
     */
    @GetMapping(value = "/get-list")
    @SysLog(operation = "获取用户", logType = LogTypeEnum.LOG_TYPE_QUERY)
    @ApiOperation(value = "获取用户", notes = "获取所有用户")
    public ResponseEntity<ResultResponse<List<UserEntity>>> getUserList() {
        return ResponseEntity.ok(ResultResponse.ok(userService.list()));
    }

    /**
     * 根据id获取用户
     * @param userId 用户id
     * @return 响应结果集
     */
    @GetMapping(value = "/get-by-id")
    @SysLog(operation = "根据id获取用户", logType = LogTypeEnum.LOG_TYPE_QUERY)
    @ApiOperation(value = "根据id获取用户", notes = "通过主键id去查询用户，返回单个用户信息")
    public ResponseEntity<ResultResponse<UserEntity>> getUserById(@ApiParam(value = "用户id", required = true)
                                                                  @RequestParam(value = "userId") Integer userId) {
        return ResponseEntity.ok(ResultResponse.ok(userService.getById(userId)));
    }
}
